<?php

$scripts=array();
$scripts_inline=array();

/**
  * add an external CSS file to the document
  *
  * @param string $url URL of the external sheet
  *
  * @return null
  */
function Core_addCSS($url) {
	global $css_urls;
	if (!is_array($css_urls)) {
		$css_urls=array();
	}
	if (in_array($url, $css_urls)) {
		return;
	}
	$css_urls[]=$url;
}

/**
  * add an external JS script to the document
  *
  * @param string $url URL of the external script
  *
  * @return null
  */
function Core_addScript($url) {
	global $scripts;
	if (in_array($url, $scripts)) {
		return;
	}
	$scripts[]=$url;
}

/**
  * add a fragment of script to a list to be shown at the end of the document
  *
  * @param string $script the JS fragment to add
  *
  * @return null
  */
function Core_addInlineScript($script) {
	global $scripts_inline;
	$script=preg_replace(
		'/\s+/',
		' ',
		str_replace(array("\n","\r"), ' ', $script)
	);
	if (in_array($script, $scripts_inline)) {
		return;
	}
	$scripts_inline[]=$script;
}

/**
  * generate a list of external CSS scripts and build a <style> tag
  *
  * @return string the HTML
  */
function Core_getCSS() {
	global $css_urls;
	if (!is_array($css_urls)) {
		return;
	}
	$url='';
	foreach ($css_urls as $s) {
		//$url.='|'.$s;
		return '<link rel="stylesheet" href="'.htmlspecialchars($s).'" />';
	}
}

/**
  * generate a list of external JS scripts and build a <script> tag
  *
  * @return string the HTML
  */
function Core_getScripts() {
	global $scripts,$scripts_inline;
	if (!count($scripts)) {
		return '';
	}
	$inline=count($scripts_inline)
		?'<script>'.join('', $scripts_inline).'</script>'
		:'';
	return '<script src="'.join('"></script><script src="', $scripts)
		.'"></script>'.$inline;
}


